package com.driving.single.common.school.service.impl;

import com.driving.single.common.domain.TbSchool;
import com.driving.single.common.entity.PageResult;
import com.driving.single.common.mapper.CustomFindItemsMapper;
import com.driving.single.common.mapper.TbSchoolMapper;
import com.driving.single.common.school.service.CommonSchoolService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class CommonSchoolServiceImpl implements CommonSchoolService {
    @Autowired
    private TbSchoolMapper tbSchoolMapper;
    @Autowired
    private CustomFindItemsMapper customFindItemsMapper;
    /**
     * 根据主键集合查询驾校
     * @param ids 主键集合
     */
    @Override
    public List<TbSchool> selectTbSchoolListByIds(List<Long> ids) {
        Example example = new Example(TbSchool.class);
        example.createCriteria().andIn("id",ids);
        return this.tbSchoolMapper.selectByExample(example);
    }
    /**
     * 根据主键查询驾校
     * @param id 主键
     */
    @Override
    public TbSchool selectTbSchoolById(Long id) {
        return this.tbSchoolMapper.selectByPrimaryKey(id);
    }

    /**
     * 找驾校页面 - 分页+条件 查询驾校
     * @param page        当前页
     * @param rows        页显示数
     * @param area        查询条件：区域(学校名称)
     * @param licenseType 查询条件：驾照类型
     * @param minPrice    查询条件：最小价格
     * @param maxPrice    查询条件：最大价格
     */
    @Override
    public PageResult selectTbSchoolListByConditionWithPage(Integer page,
                                                            Integer rows,
                                                            String area,
                                                            String licenseType,
                                                            Integer minPrice,
                                                            Integer maxPrice) {
        PageHelper.startPage(page, rows);

        List<TbSchool> tbSchools = this.customFindItemsMapper.selectTbSchoolListByConditionWithPage(area,licenseType,
                minPrice, maxPrice);

        PageInfo<TbSchool> pageInfo = new PageInfo<>(tbSchools);

        PageResult pageResult = new PageResult();
        pageResult.setResult(tbSchools);
        pageResult.setPageIndex(page);
        pageResult.setTotalPage(pageInfo.getPages());
        pageResult.setTotalSize(pageInfo.getTotal());

        return pageResult;
    }
}
